package com.banciyuan.bcywebview.biz.main.group.hot.viewholder;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.biz.groupdetail.NewTeamDetailActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import de.greenrobot.daoexample.model.TagDetail;
import de.greenrobot.daoexample.model.Team;

/**
 * Created by squall on 2017/4/21.
 */

public class ViewGroupBase {

    public TextView title;
    public TextView bestPost;
    public TextView post_num;
    public TextView tag_text;
    public View workForm;
    public String joinFormat;
    public String talkGroupFormat;
    public String joinGroupFormat;

    public ViewGroupBase(View convertView) {
        title = (TextView) convertView.findViewById(R.id.group_new_item_title_one);
        post_num = (TextView) convertView.findViewById(R.id.tv_postnum);
        bestPost = (TextView) convertView.findViewById(R.id.talk_intro);
        tag_text = (TextView) convertView.findViewById(R.id.tag_text);
        workForm = convertView.findViewById(R.id.from_mark);
    }

    public void renderData(final Context mContext, ViewGroupBase viewGroupBase, final Team team, int type) {
        renderGroupTagData(mContext, viewGroupBase.tag_text, viewGroupBase.workForm, team);
        setPostNum(viewGroupBase, team, type);
        if (!TextUtils.isEmpty(team.getName())) {
            viewGroupBase.title.setText(Html.fromHtml(team.getName()));
        }
        if (team.getPosts() != null && !team.getPosts().isEmpty()) {
            viewGroupBase.bestPost.setVisibility(View.VISIBLE);
            viewGroupBase.bestPost.setText(Html.fromHtml(team.getPosts().get(0).getPlain()));
        } else {
            viewGroupBase.bestPost.setVisibility(View.GONE);
        }

        ((View) (viewGroupBase.title.getParent())).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, NewTeamDetailActivity.class, team.getGid(), team.getName());
            }
        });
    }

    private void setPostNum(ViewGroupBase viewGroupBase, Team team, int type) {
        if (!TextUtils.isEmpty(team.getPost_num())) {
            int postNum = Integer.parseInt(team.getPost_num());
            if (type == -1) {
                if (postNum > 10) {
                    viewGroupBase.post_num.setText(String.format(talkGroupFormat, team.getPost_num()));
                } else {
                    viewGroupBase.post_num.setText(joinFormat);
                }
            } else if (type == 2) {
                if (postNum > 0) {
                    viewGroupBase.post_num.setText(String.format(joinGroupFormat, team.getPost_num()));
                } else {
                    viewGroupBase.post_num.setText(joinFormat);
                }
            } else {
                viewGroupBase.post_num.setText(String.format(talkGroupFormat, team.getPost_num()));
            }
        } else {
            viewGroupBase.post_num.setText(joinFormat);
        }
    }

    public void renderGroupTagData(final Context mContext, TextView tag_text, View workForm, final Team team) {
        if (!team.getTags().isEmpty()) {
            tag_text.setVisibility(View.VISIBLE);
            workForm.setVisibility(View.VISIBLE);
            final TagDetail t = team.getTags().get(0);
            tag_text.setText(t.getTag_name());
            if (!TextUtils.isEmpty(t.getWork()) && !isLegalWork(t.getWid())) {
                tag_text.setTextColor(mContext.getResources().getColor(R.color.my_like_name));
            } else {
                tag_text.setTextColor(mContext.getResources().getColor(R.color.timeline_green));
            }
            ((View) (tag_text.getParent())).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!TextUtils.isEmpty(t.getWork())) {
                        if (isLegalWork(t.getWid())) {
                            Intent intent = new Intent(mContext, CircleSmoothActivity.class);
                            intent.putExtra(gotoUtil.INTENT_VALUE_TWO, team.getId());
                            intent.putExtra(gotoUtil.INTENT_VALUE_ONE, team.getWork());
                            intent.putExtra("from", 2);
                            mContext.startActivity(intent);
                        }
                    } else {
                        Intent intent = new Intent(mContext, CircleTagSmoothActivity.class);
                        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, t.getTag_name());
                        intent.putExtra("type", "tag");
                        intent.putExtra("from", 2);
                        mContext.startActivity(intent);
                    }
                }
            });
            talkGroupFormat = mContext.getString(R.string.person_talk_group_unit);
            joinGroupFormat = mContext.getString(R.string.person_join_group_unit);
            joinFormat = mContext.getString(R.string.wait_for_you_join_n);
        } else {
            tag_text.setVisibility(View.GONE);
            workForm.setVisibility(View.GONE);
            talkGroupFormat = mContext.getString(R.string.person_talk_group_unit_2);
            joinGroupFormat = mContext.getString(R.string.person_join_group_unit_2);
            joinFormat = mContext.getString(R.string.wait_for_you_join);
        }
    }

    private boolean isLegalWork(String wid) {
        try {
            if (TextUtils.isEmpty(wid) || "false".equals(wid) || Integer.valueOf(wid) <= 0)
                return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
